import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import Login from '../pages/login/login.vue'
import Tabs from '../pages/tabs/index.vue'
import Project from '../pages/tabs/project/index.vue'
import ProjectUser from '../pages/tabs/project/project_user.vue'
import Customer from '../pages/tabs/customer/customer.vue'
import User from '../pages/tabs/user/user.vue'
import createProject from '../pages/tabs/project/createProject.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      redirect: () => {
        // 初始化用户状态
        const userStore = useUserStore()
        userStore.initUserState()

        // 如果已登录，跳转到项目列表页面
        if (userStore.hasLogin) {
          return '/tabs/project'
        }
        // 否则跳转到登录页
        return '/login'
      },
    },
    {
      path: '/login',
      name: 'loginPage',
      component: Login,
      meta: { requiresGuest: true }, // 只有未登录用户才能访问
    },
    {
      path: '/tabs',
      name: 'tabs',
      component: Tabs,
      meta: { requiresAuth: true }, // 需要登录才能访问
      redirect: '/tabs/project',
      children: [
        {
          path: 'createProject',
          name: 'createProject',
          component: createProject,
          meta: { requiresAuth: true },
        },
        {
          path: 'project',
          name: 'project',
          component: Project,
          meta: { requiresAuth: true },
        },
        {
          path: 'project/detail',
          name: 'projectDetail',
          component: () => import('../pages/tabs/project/project.vue'),
          meta: { requiresAuth: true },
        },
        {
          path: 'project_user',
          name: 'project_user',
          component: ProjectUser,
          meta: { requiresAuth: true },
        },
        {
          path: 'customer',
          name: 'customer',
          component: Customer,
          meta: { requiresAuth: true },
        },
        {
          path: 'user',
          name: 'user',
          component: User,
          meta: { requiresAuth: true },
        },
      ],
    },
    {
      path: '/role-detail/:rowid?',
      name: 'roleDetail',
      component: () => import('../pages/customer/RoleDetailDialog.vue'),
      meta: { requiresAuth: false }, // 角色pdf
    },
    {
      path: '/role-detail-word/:rowid?',
      name: 'roleDetailWord',
      component: () => import('../pages/customer/RoleDetailWord.vue'),
      meta: { requiresAuth: false }, // 明道云快照功能不需要登录
    },
    {
      path: '/video',
      name: 'video',
      component: () => import('../pages/customer/video.vue'),
      meta: { requiresAuth: false }, // 视频播放页面不需要登录
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 初始化用户状态（从localStorage恢复）
  userStore.initUserState()

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    if (userStore.hasLogin) {
      next() // 已登录，继续访问
    } else {
      next('/login') // 未登录，跳转到登录页
    }
  }
  // 检查是否只允许未登录用户访问（如登录页）
  else if (to.meta.requiresGuest) {
    if (userStore.hasLogin) {
      next('/tabs/project') // 已登录，跳转到项目列表页面
    } else {
      next() // 未登录，继续访问
    }
  }
  // 其他页面直接访问
  else {
    next()
  }
})

export default router
